<?php

namespace app\business;

use app\library\facade\Es;

class EsBusiness
{
    public function createIndex()
    {
        $indices = @Es::indices();
        //判断索引是否存在
        $res = $indices->exists(['index' => 'hjsc']);

        if (!$res) {
            //如果不存在则创建
            try {
                $res = $indices->create([
                    'index' => "hjsc",
                    'body' => [
                        'settings' => [//配置
                            'number_of_shards' => 2,//主分片数
                            'number_of_replicas' => 1, //主分片的副本数
                            'index.max_ngram_diff' => 5,
                            'analysis' => [
                                'filter' => [
                                    'ngram_filter' => [
                                        'type' => 'ngram',
                                        'min_gram' => 2,
                                        'max_gram' => 6
                                    ]
                                ],
                                'analyzer' => [
                                    'ngram_analyzer' => [
                                        'type' => "custom",
                                        'tokenizer' => "whitespace",
                                        'filter' => [
                                            'lowercase',
                                            'ngram_filter'
                                        ]
                                    ]
                                ]
                            ]
                        ],
                        'mappings' => [
                            '_source' => [
                                'enabled' => true
                            ],
                            'properties' => [
                                'common_id' => [
                                    'type' => 'integer'
                                ],
                                'common_name' => [
                                    'type' => 'text',
                                    'analyzer' => 'ik_max_word',
                                    'search_analyzer' => 'ik_max_word',
                                ]
                            ]
                        ]
                    ]
                ]);
            } catch (\Exception $e) {
                var_dump($e->getMessage());
            }
        }
    }
}